\documentclass{ctexart}

\usepackage{geometry}%设置页面大小边距等
\usepackage{authblk}%作者机构等信息
\usepackage{graphicx}%插入图片
\usepackage{amssymb}%为了用\mathbb
\usepackage{amsmath}%数学方程的显示
\usepackage{listings}%插入代码
\usepackage{fancyhdr}%设置页眉页脚
\usepackage{lastpage}%总页数
\usepackage{tikz}
\usepackage{listings}%插入代码
\usepackage{hyperref}%引用网页
\usepackage{xcolor}%实现代码颜色高亮
\lstset{frame=single,basicstyle=\ttfamily,columns=flexible,language={C},
keywordstyle=\bf\color{blue},
commentstyle=\it\color[RGB]{0,96,96},
numberstyle=\color[RGB]{0,192,192}}

\geometry{a4paper,left=2cm,right=2cm,top=2cm,bottom=2cm}%一定要放在前面！

\pagestyle{fancy}%设置页眉页脚
\lhead{陈冠宇\ 3200102033}%页眉左
\chead{Numerical Analysis}%页眉中
\rhead{HW2\ Report}%章节信息
\cfoot{\thepage/\pageref{LastPage}}%当前页，记得调用前文提到的宏包
\rfoot{School of Mathematical Sciences in ZJU}
\renewcommand{\headrulewidth}{0.1mm}%页眉线宽，设为0可以去页眉线
\renewcommand{\footrulewidth}{0.1mm}%页脚线宽，设为0可以去页眉线
\setlength{\headwidth}{\textwidth}

\newtheorem{theorem}{Theorem}
\newtheorem{proof}{Proof}
\newtheorem{solution}{Solution:}

\hypersetup{%设置网页链接颜色等
    colorlinks=true,%链接将会有颜色，默认是红色
    urlcolor=blue,%链接到网站的链接将会变为蓝绿色（cyan）
    }

\title{Report of Programming Assignments HW2}
\begin{document}

\section*{Report of Programming Assignments}
\subsection*{Introduction}
The programs below are made for solving the root of some equations by bisection method, Newton method and secant method respectively.

In EquationSolver.h, we create the class Bisection, class Newton and class Secant, also with the virtual class Function and EquationSolver.

In the class function however, we need to change the class function every time we meet a new function.

The programs assignmentsB(C,D,E,F).cpp are used for solving problem B,C,D,E,F respectively. The result of each problem, including the code, and conclusion will be explained as follows.

{\bf In the program, input: make, then compile it. ./proX output the result of assignmentsX.}
\subsection*{\expandafter{A.EquationSolver.h}}
\subsubsection*{Virtual class Function:}
\begin{lstlisting}
class Function{
 public:
  virtual double operator()(double _x){
    return 0;
  }
  virtual double diff(double _x){
    return 0;
  }
};
\end{lstlisting}
\subsubsection*{class EquationSolver:}
\begin{lstlisting}
class EquationSolver{
 public:
  virtual double solve()=0;
  //used for the same interface in different solving methods.
};
\end{lstlisting}
\subsubsection*{Bisection Method}
\begin{lstlisting}
 private:
  double a,b,delta,eps;
  int M;
  Function &f;
 public:
 Bisection();//initialize function
 Bisection(Function &_f, double _a, double _b, double _delta, double _eps, int _M);
  double solve();
\end{lstlisting}
\emph{\bf Remark:} {Here I want to thank for Wenchong Huang's help. He explained the difference between initialization and initial-value-given to me patiently. That is i need to define a Function class 'func' outside the method class before the citation of f. Otherwise, If i give the initial value to f directly, it will go wrong.}
\subsubsection*{Newton Method:}
\begin{lstlisting}
 private:
  double x0,eps;
  int M;
  Function &f;
 public:
 Newton();
 Newton(Function &_f, double _x0, double _eps, int _M);
\end{lstlisting}
\subsubsection*{Secant Method:}
\begin{lstlisting}
 private:
  double x0, x1, delta, eps;
  int M;
  Function &f;
 public:
 Secant();
 Secant(Function &_f, double _x0, double _x1, double _delta, double _eps, int _M);
 double solve();
\end{lstlisting}


\subsection*{B,C,D,E Similar to B}
Firstly, we define the const variation pi and the machine precision "eps".
\begin{lstlisting}
double pi = acos(-1);//Thank for Wenchong Huang's instruction.
double eps = numeric_limits<double>::epsilon();
\end{lstlisting}
Then we create a derived class "Func" inherited from the predefined class "Function". In this class we define specific function and its derivatives.
\begin{lstlisting}
  double operator()(double x);
  double diff(double x);
\end{lstlisting}
Finally in the test program, we deliver the parameters to the function method.solve() and print out the result.
\subsection*{Result:}
\subsubsection*{B.}
\begin{lstlisting}
The root of function 1/x-tan(x) is 0.860334
The root of function 1/x-2^x is 0.641186
The root of function 2^{-x}+exp{x}+2cos(x)-6 is 1.82938
The root of function (x^3+4x^2+3x+5)/(2x^3-9x^2+18x-2) is 0.117877
\end{lstlisting}
\subsubsection*{C.}
\begin{lstlisting}
Newton: The root of function x-tan(x) at 4.5 is 4.49341
Newton: The root of function x-tan(x) at 7.7 is 7.72525
\end{lstlisting}
\subsubsection*{D.}
\begin{lstlisting}
The root of sin(x/2)-1 in 0~pi/2 is 3.14159
The root of exp(x)-tan(x) in 1~1.4 is 1.30633
The root of pow(x,3)-12*pow(x,2)+3*x+1 in -0.5~1 is -0.188685
\end{lstlisting}
\subsubsection*{E.}
\begin{lstlisting}
(Bisection Method) The depth of water is 0.833834
(Newton  Method) The depth of water is 0.833834
(Secant Method) The depth of water is 0.833834
\end{lstlisting}
\subsubsection*{F.}
\begin{lstlisting}
(a)Alpha = 33.3947
(b)Alpha = 33.3977
(c)Alpha = 33.3947 with pi*33/180, pi*60/180
(c)Alpha = 358.17 with 10000, pi*33/180
\end{lstlisting}
When the angle = 33, the scant value cannot approach the tangent value. Then the result is not right.
\subsection*{Conclusion}
Source code: \href{https://gitee.com/Zebrainy-cgy/na2022/tree/master/HW2/src}
{https://gitee.com/Zebrainy-cgy/na2022/tree/master/HW2/src}

\end{document} 